﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;
using System.Web.Configuration;
using System.Configuration;

public partial class Companys_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            PermissionHelper hh = new PermissionHelper();
            Session["Permission"] = hh.GetPermission(EUserRole.superadmin);
            btnSave.Visible = false;
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsCompanyEdit)
                {
                    btnSave.Visible = true;
                     
                }
                if (Request.Params["cid"] != null)
                {
                    try
                    {
                        int cid = Convert.ToInt32(Request.Params["cid"]);
                        LoadCompany(cid);
                        hCid.Value = Request.Params["cid"];
                    }
                    catch
                    {
                    }
                }

            }

        }
    }

    void LoadCompany(int cid)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;

            CompanyBLL cbll = new CompanyBLL(permission);
            Company company = cbll.GetCompany(cid);
            txtAddress.Text = company.Address;
            txtCompanyName.Text = company.CompanyName;
            txtContact.Text = company.Contact;
            txtContactPerson.Text = company.ContactPerson;
            txtCurrentIntegral.Text = company.CurrentIntegral.ToString();
            txtEmail.Text = company.Email;
            txtGuardLine.Text = company.GuardLine.ToString();
            txtPhone.Text = company.Phone;

            txtxStatisticalStartDate.Text = company.StatisticalStartDate.ToString("yyyy-MM-dd");
            if (company.IsAutoRecharge == 1)
            {
                rbAutoRecharge.Checked = true;
            }
            else
            {
                rbUnAutoRecharge.Checked = true;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;

            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            string mainCompanyName = config.AppSettings.Settings["maincompany"].Value;

            if (permission != null && txtCompanyName.Text.Trim() != mainCompanyName)
            {
                CompanyBLL cbll = new CompanyBLL(permission);
                cbll.MainCompanyName = mainCompanyName;

                Company company = cbll.GetCompany(Convert.ToInt32(hCid.Value));

                company.Address = txtAddress.Text;
                company.CompanyName = txtCompanyName.Text;
                company.Contact = txtContact.Text;
                company.ContactPerson = txtContactPerson.Text;
                company.CurrentIntegral = Convert.ToInt32(txtCurrentIntegral.Text);
                company.Email = txtEmail.Text;
                company.Phone = txtPhone.Text;
                company.GuardLine = Convert.ToInt32(txtGuardLine.Text);
                if (rbAutoRecharge.Checked)
                {
                    company.IsAutoRecharge = 1;
                }

                if (rbUnAutoRecharge.Checked)
                {
                    company.IsAutoRecharge = 0;
                }

                
                company.StatisticalStartDate = Convert.ToDateTime(txtxStatisticalStartDate.Text);
                 
                cbll.UpdateCompany(company);

                Response.Write("<script> alert( '提交成功！ ');location= 'Index.aspx'</script> ");

            }
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}
